package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.dto.SetmealDto;
import com.reggie.entity.Category;
import com.reggie.entity.Setmeal;
import com.reggie.service.CategoryService;
import com.reggie.service.SetMealService;
import com.reggie.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;



//    新增套餐
    @PostMapping
    @CacheEvict(value = "SetMeal",allEntries = true)
    public R<String> save(@RequestBody SetmealDto  setmealDto){
        log.info("套餐信息: {}",setmealDto.toString());
       setMealService.saveWithDish(setmealDto);
        return R.success("新增成功");
    }

//    分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage=new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setMealService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();

       List<SetmealDto> setmealDtos= records.stream().map((item)->{
            SetmealDto setmealDto=new SetmealDto();
            Category category = categoryService.getById(item.getCategoryId());
            if (category!=null){
                setmealDto.setCategoryName(category.getName());
                BeanUtils.copyProperties(item,setmealDto);
            }
            return setmealDto;

        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtos);
          return R.success(setmealDtoPage);
    }

//    删除套餐
    @DeleteMapping
    @CacheEvict(value = "SetMeal",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        setMealService.removeWithDish(ids);
        return R.success("删除成功");
    }

//    修改套餐的售卖状态
    @PostMapping("/status/{status}")
    @CacheEvict(value = "SetMeal",allEntries = true)
    public R<String> update(@PathVariable("status") int status,@RequestParam List<Long> ids){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        List<Setmeal> list = setMealService.list(queryWrapper);
        if (list!=null){
            for (Setmeal setmeal:list){
                setmeal.setStatus(status);
                setMealService.updateById(setmeal);
            }
        }
        return R.success("状态修改成功");
    }

//    根据条件查询套餐数据
    @GetMapping("/list")
    @Cacheable(value = "SetMeal",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> setmealList = setMealService.list(queryWrapper);

        return R.success(setmealList);

    }
}
